#include "sdcard.h"
#include "protocol.h"

struct sdcard_file_struct sdcard = { 0, 0, "komunikaty.txt" };

extern struct Params sParams;


//////////////////////////////vWriteToFile/////////////////////////////////////////////////////

void vWriteToFile(int32_t offset_value, struct fat16_fs_struct* fs, struct fat16_dir_struct* dd, char* buf)
{

	/* search file in current directory and open it */
	struct fat16_file_struct* fd = open_file_in_dir(fs, dd, sdcard.file);
	if(!fd) {
	}

	//int32_t offset = strtolong(offset_value);
	if(!fat16_seek_file(fd, &offset_value, FAT16_SEEK_SET)) {
		fat16_close_file(fd);    
	}

	/* read text from the shell and write it to the file */
	uint8_t data_len;
	/* read one line of text */
	data_len =  sizeof(*buf);

	/* write text to file */
	while(!(*buf=='\0')) {
		fat16_write_file(fd, buf, data_len);
		buf++;
	}
	fat16_write_file(fd,  "\r\n", 2);

	fat16_close_file(fd);

}


////////////////////////////////vReadFile///////////////////////////////////////////

void vReadFile(struct fat16_fs_struct* fs, struct fat16_dir_struct* dd,unsigned char ucLineNumber,uint8_t cLineDestination[])   
{
         
	uint8_t i, buffer[17];
	unsigned char ucLicznikLini = 0;

	/* search file in current directory and open it */
	struct fat16_file_struct* fd = open_file_in_dir(fs, dd, sdcard.file);
	if(!fd) {
	}

	/* print file contents */
	while(fat16_read_file(fd, buffer, sizeof(buffer)) > 0) {
              
		if(ucLicznikLini== ucLineNumber) {

			for(i = 0; i < 15; ++i) {
				cLineDestination[i] = buffer[i];
			}
			cLineDestination[i] =0;
		} 
			  
		ucLicznikLini++;
	}

	fat16_close_file(fd);

}

////////////////////////////////////////////////////////////////////////////////////////////

struct fat16_file_struct* open_file_in_dir(struct fat16_fs_struct* fs, struct fat16_dir_struct* dd, const char* name)
{
    struct fat16_dir_entry_struct file_entry;
    if(!find_file_in_dir(fs, dd, name, &file_entry))
        return 0;

    return fat16_open_file(fs, &file_entry);

}

////////////////////////////////////////////////////////////////////////////

uint8_t find_file_in_dir(struct fat16_fs_struct* fs, struct fat16_dir_struct* dd, const char* name, struct fat16_dir_entry_struct* dir_entry)
{
    while(fat16_read_dir(dd, dir_entry))
    {
        if(strcmp(dir_entry->long_name, name) == 0)
        {
            fat16_reset_dir(dd);
            return 1;
        }
    }

    return 0;
}

//////////////////////////////////////////////////////////////////////////

uint64_t SDcard_GetNumberOfLines(void) 
{ 

	uint64_t ucNumberOfLines = 0;
	uint8_t buffer[17];

	/* search file in current directory and open it */
	struct fat16_file_struct* fd = open_file_in_dir(sdcard.fs, sdcard.dd, sdcard.file);

    if(!fd) {
    }

    /* print file contents */

	while(fat16_read_file(fd, buffer, sizeof(buffer)) > 0) {
				ucNumberOfLines++;
	}
	fat16_close_file(fd);

	return ucNumberOfLines/2;
} 

void SDcard_ReadLine(uint64_t uiLineNumber, uint8_t cLineDestination[]) { 
	
	vReadFile(sdcard.fs, sdcard.dd, uiLineNumber, cLineDestination);						
} 

void SDcard_WriteMsg( char* buff) { 
	
	vWriteToFile(sParams.uiNumberOfMsg*2*17, sdcard.fs, sdcard.dd, "23-06-09 15:31 ");
	vWriteToFile(sParams.uiNumberOfMsg*2*17+17, sdcard.fs, sdcard.dd, buff);

} 
